package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	
	import icbc.app.AppGlobal;
	
	import org.osflash.signals.Signal;
	
	public class ContentNavButtonControl extends Sprite
	{
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var main:Sprite = new Sprite();
		private var base:Sprite = new Sprite();
		private var over:Sprite = new Sprite();
		private var blight:Sprite = new Sprite();
		private var press:Sprite = new Sprite();
		private var panel:Sprite;
		
		public var onClick:Signal = new Signal();
		public var onBlinkStop:Signal = new Signal();
				private var tweenBlink:TweenLite;
		
		public var isBlinkStop:Boolean = false;

		public function ContentNavButtonControl(vWidth:uint, vHeight:uint)
		{
			super();
			this.controlWidth = vWidth;
			this.controlHeight = vHeight;
			addChild(main);
			main.addChild(base);
			main.addChild(over);
			main.addChild(blight);
			main.addChild(press);
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			display();
		}
		private function display():void
		{
			buildBase();
			buildOver();
			buildBlink();
			buildPress();
			addListeners();
		}
		
		private function build(item:Sprite, color:uint = 0x1f416c):void
		{
			setListItemBase(item, color);
		}
		
		private function setListItemBase(item:Sprite, baseColor:uint = 0x1f416c):void
		{
			item.graphics.beginFill(0xffffff,0);
			item.graphics.drawRect(0, 0, controlWidth, controlHeight);
			item.graphics.endFill();
			item.alpha = 0;
			
			var arrow:Sprite = buildArrow(baseColor, controlWidth, controlHeight-5);
			item.addChild(arrow);
			arrow.x = controlWidth/2 - arrow.width/2;
			arrow.y = controlHeight/2 - arrow.height/2;
		}

		public function buildArrow(color:uint, arrowWidth:int, arrowHeight:int):Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.graphics.lineStyle(3, color);
			sprite.graphics.lineTo(0,0);
			sprite.graphics.lineTo(arrowWidth,arrowHeight/2);
			sprite.graphics.lineTo(0,arrowHeight);
			return sprite;
		}
		
		private function addListeners():void
		{
			press.addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.addEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function removeListeners():void
		{
			press.removeEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.removeEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.removeEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function onRollOver(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,1,{ease:Strong.easeOut,alpha:1});
			isBlinkStop = true;
			onBlinkStop.dispatch();
		}
		
		private function onRollOut(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,2,{ease:Strong.easeOut,alpha:0});
			press.alpha = 0;
		}
		
		private function onPress(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,0.5,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRelease(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,1,{ease:Strong.easeOut,alpha:0});
			onClick.dispatch();
		}
		
		private function buildBase():void
		{
			build(base, 0xC1C1B7);
			base.alpha = 1;
		}
		
		private function buildOver() : void
		{
			build(over, 0x9E9E94);
		}

		private function buildBlink() : void
		{
			build(blight, 0x9E9E94);
			var filterD:DropShadowFilter = new DropShadowFilter(0, 45, 0x999999, 1, 10, 10, 2);
			blight.filters = [ filterD ];
		}
		
		private function buildPress() : void
		{
			build(press, AppGlobal.RED_ICBC);
		}
		
		public function dispose():void
		{
			removeListeners();
		}
		
		public function blink():void
		{
			initBlink();
			isBlinkStop = false;
		}
		
		private function initBlink():void
		{
			if(isBlinkStop) return;
			tweenBlink = new TweenLite(main,0.8,{ease:Strong.easeOut,alpha:0.5, onComplete:onBlinkComplete});
		}
		
		private function onBlinkComplete():void
		{
			var tween:TweenLite = new TweenLite(main,1,{ease:Strong.easeOut,alpha:1, onComplete:initBlink});
		}
		
		public function stopBlink():void
		{
			isBlinkStop = true;
		}
	}
}